package com.wyh.controller.groupOne.organization;

import com.github.pagehelper.PageInfo;
import com.wyh.common.result.Result;

import com.wyh.domain.po.groupOne.MasterDataUser;
import com.wyh.domain.po.groupOne.organization.MasterDataOrganization;
import com.wyh.domain.vo.groupOne.utils.MasterDataTreeNode;
import com.wyh.service.groupOne.organization.MasterDataOrganizationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.wyh.common.enums.ResultCodeEnum.*;

@RestController
@RequestMapping("/organization")
@Api(tags = "组织管理API")
public class MasterDataOrganizationController {

    @Autowired
    private MasterDataOrganizationService masterDataOrganizationService;

    // 获取菜单树的接口
    @ApiOperation(value = "获取组织管理菜单树的功能")
    @GetMapping("/getOrganizationTree")
    public Result getOrganizationTree(@RequestHeader(value = "authorization", required = false) String authorization) {
        List<MasterDataTreeNode> tree = masterDataOrganizationService.generateOrganizationTree();
        if (tree == null) {
            Result.error(SYSTEM_ERROR);
        }
        return Result.success(tree);
    }

    // 添加组织的接口
    @ApiOperation(value = "添加组织的功能")
    @ApiImplicitParam(name = "masterDataOrganization", value = "组织", required=true)
    @PostMapping("/addOrganization")
    public Result addOrganization(@RequestBody @Validated MasterDataOrganization masterDataOrganization,
                                  @RequestHeader(value = "authorization", required = false) String authorization) {
        if (masterDataOrganization == null) {
            return Result.error(ORGANIZATION_ADD_ERROR);
        }
        boolean isAddSuccess = masterDataOrganizationService.addOrganization(masterDataOrganization);
        if (isAddSuccess) {
            return Result.success(ADD_SUCCESS);
        } else {
            return Result.error(ORGANIZATION_ADD_ERROR);
        }
    }

    // 用户点击菜单树组织查找数据的接口
    @ApiOperation(value = "点击菜单树的单项-查询组织信息的功能")
    @ApiImplicitParam(name = "id", value = "组织id", required=true)
    @GetMapping("/getOrganization")
    public Result getOrganization(@RequestParam("id") Integer id,
                                  @RequestHeader(value = "authorization", required = false) String authorization) {
        if (id == null) {
            return Result.error(ORGANIZATION_FIND_ERROR);
        }
        MasterDataOrganization masterDataOrganization = masterDataOrganizationService.findOrganizationById(id);
        return Result.success(masterDataOrganization);
    }

    // 用户点击编辑按钮执行的接口
    @ApiOperation(value = "编辑组织的功能")
    @ApiImplicitParam(name = "masterDataOrganization", value = "组织", required=true)
    @PostMapping("/editOrganization")
    public Result editOrganization(@RequestBody MasterDataOrganization masterDataOrganization,
                                   @RequestHeader(value = "authorization", required = false) String authorization) {
        if (masterDataOrganization == null) {
            return Result.error(ORGANIZATION_UPDATE_ERROR);
        }
        boolean isUpdateSuccess = masterDataOrganizationService.editOrganization(masterDataOrganization);
        if (isUpdateSuccess) {
            return Result.success(UPDATE_SUCCESS);
        } else {
            return Result.error(ORGANIZATION_UPDATE_ERROR);
        }
    }

    // 用户删除一个组织的接口
    @ApiOperation(value = "删除组织的功能")
    @ApiImplicitParam(name = "id", value = "组织id", required=true)
    @GetMapping("/removeOrganization")
    public Result removeOrganization(@RequestParam("id") Integer id,
                                     @RequestHeader(value = "authorization", required = false) String authorization) {
        if (id == null) {
            return Result.error(ORGANIZATION_FIND_ERROR);
        }
        boolean isRemoveSuccess = masterDataOrganizationService.removeOrganization(id);
        if (!isRemoveSuccess) {
            return Result.error(ORGANIZATION_REMOVE_ERROR);
        }
        return Result.success(ORGANIZATION_REMOVE_SUCCESS);
    }

    // 查询组织下所有用户的接口，这个id是页面当前展示的数据的id
    @ApiOperation(value = "查询当前组织下的所有用户的功能")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "组织id", required=true),
            @ApiImplicitParam(name = "pageNum", value = "页码", required=true, dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "一页包含的数据量", required=true, dataType = "int")
    })
    @GetMapping("/getUsers")
    public Result getUsersByOrganizationId(@RequestParam("id") Integer id,
                                           @RequestParam(defaultValue = "1") int pageNum,
                                           @RequestParam(defaultValue = "10") int pageSize,
                                           @RequestHeader(value = "authorization", required = false) String authorization) {
        if (id == null) {
            return Result.error(ORGANIZATION_FIND_ERROR);
        }
//        List<MasterDataUser> users = masterDataOrganizationService.findUsers(id, pageNum, pageSize);
        PageInfo<MasterDataUser> users = masterDataOrganizationService.findUsers(id, pageNum, pageSize);
        if (users.getList().isEmpty()) {
            return Result.error(ORGANIZATION_FIND_ERROR);
        }
        return Result.success(users);
    }

    // 获取模糊查询前端实时响应的树的接口
    @ApiOperation(value = "搜索组织时菜单树动态变化的功能")
    @ApiImplicitParam(name = "keyword", value = "关键词", required=true)
    @GetMapping("/searchOrganizationTree")
    public Result searchOrganizationTree(@RequestParam("keyword") String keyword,
                                         @RequestHeader(value = "authorization", required = false) String authorization) {
        if (keyword == null) {
            return Result.error(ORGANIZATION_FIND_ERROR);
        }
        List<MasterDataTreeNode> organizations = masterDataOrganizationService.searchOrganizationTree(keyword);
        if (organizations == null || organizations.isEmpty()) {
            return Result.success(KEYWORDS_ERROR);
        }
        return Result.success(organizations);
    }

}
